package com.axiang.module_sign.ui;

import android.animation.ObjectAnimator;

import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.annotation.Nullable;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

import android.view.animation.LinearInterpolator;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.axiang.lib_common.mvvm.BaseMvvmActivity;
import com.ren.lib_path.router.RouterActivityPath;
import com.axiang.lib_common.utils.RxAnimationTool;
import com.axiang.lib_common.utils.RxBarTool;
import com.axiang.lib_common.utils.RxDeviceTool;
import com.axiang.module_sign.BR;
import com.axiang.module_sign.R;
import com.axiang.module_sign.databinding.ActivityLoginBinding;
import com.axiang.module_sign.factory.SignViewModelFactory;
import com.axiang.module_sign.viewmodel.LoginViewModel;

@Route(path = RouterActivityPath.Sign.PAGER_LOGIN)
public class LoginActivity extends BaseMvvmActivity<ActivityLoginBinding, LoginViewModel> {

    private int keyHeight = 0; //软件盘弹起后所占高度


    @Override
    public int onBindLayout() {
        return R.layout.activity_login;
    }

    @Override
    public boolean enableToolbar() {
        return false;
    }

    @Override
    public Class<LoginViewModel> onBindViewModel() {
        return LoginViewModel.class;
    }

    @Override
    public ViewModelProvider.Factory onBindViewModelFactory() {
        return SignViewModelFactory.getInstance(getApplication());
    }

    @Override
    public void initViewObservable() {
        //监听ViewModel中pSwitchObservable的变化, 当ViewModel中执行【uc.pSwitchObservable.set(!uc.pSwitchObservable.get());】时会回调该方法
        mViewModel.uc.pSwitchEvent.observe(this, new Observer<Boolean>() {
            @Override
            public void onChanged(@Nullable Boolean aBoolean) {
                //pSwitchObservable是boolean类型的观察者,所以可以直接使用它的值改变密码开关的图标
                if (mViewModel.uc.pSwitchEvent.getValue()) {
                    //密码可见
                    //在xml中定义id后,使用binding可以直接拿到这个view的引用,不再需要findViewById去找控件了
                    mBinding.ivShowPwd.setImageResource(R.drawable.pass_gone);
                    mBinding.etPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                } else {
                    //密码不可见
                    mBinding.ivShowPwd.setImageResource(R.drawable.pass_visuable);
                    mBinding.etPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
                }
            }
        });
    }

    @Override
    public int onBindVariableId() {
        return BR.viewModel;
    }

    @Override
    public void initView() {
        RxBarTool.setTransparent(this);// 状态栏透明
        RxBarTool.setLightMode(this); //白色主题

        keyHeight = RxDeviceTool.getScreenHeight(this) / 3;//弹起高度为屏幕高度的1/3
    }

    @Override
    public void initListener() {
        /**
         * 禁止键盘弹起的时候可以滚动
         */
        mBinding.scrollView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return true;
            }
        });
        mBinding.scrollView.addOnLayoutChangeListener(new ViewGroup.OnLayoutChangeListener() {
            @Override
            public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
              /* old是改变前的左上右下坐标点值，没有old的是改变后的左上右下坐标点值
              现在认为只要控件将Activity向上推的高度超过了1/3屏幕高，就认为软键盘弹起*/
                if (oldBottom != 0 && bottom != 0 && (oldBottom - bottom > keyHeight)) {
                    int dist = mBinding.content.getBottom() - bottom;
                    if (dist > 0) {
                        ObjectAnimator mAnimatorTranslateY = ObjectAnimator.ofFloat(mBinding.content, "translationY", 0.0f, -dist);
                        mAnimatorTranslateY.setDuration(300);
                        mAnimatorTranslateY.setInterpolator(new LinearInterpolator());
                        mAnimatorTranslateY.start();
                        RxAnimationTool.zoomIn(mBinding.logo, 0.6f, dist);
                    }
                    mBinding.service.setVisibility(View.INVISIBLE);

                } else if (oldBottom != 0 && bottom != 0 && (bottom - oldBottom > keyHeight)) {

                    if ((mBinding.content.getBottom() - oldBottom) > 0) {
                        ObjectAnimator mAnimatorTranslateY = ObjectAnimator.ofFloat(mBinding.content, "translationY", mBinding.content.getTranslationY(), 0);
                        mAnimatorTranslateY.setDuration(300);
                        mAnimatorTranslateY.setInterpolator(new LinearInterpolator());
                        mAnimatorTranslateY.start();
                        //键盘收回后，logo恢复原来大小，位置同样回到初始位置
                        RxAnimationTool.zoomOut(mBinding.logo, 0.6f);
                    }
                    mBinding.service.setVisibility(View.VISIBLE);
                }
            }
        });
    }
}
